import { GetUserInfo,UploadImg,GetImg } from "@/api/user.js"; //PutUserInfo
import $router from "@/router/index.js";
// import { UploadImg } from "@/api/user";
export default {
    state:{
        current:0,
        userInfo:{
            name:'mumu',
            id:3,
            score:'1020',
            rank:100,
            img:"http://dida100.com:8888//uploads/file-1660464999660.png"
        },
        headerImg:"http://dida100.com:8888//uploads/file-1660464999660.png",
        token:''
    },
    mutations:{
        SET_CURRENT(state,data){
            state.current = data
          },
        SET_SCORE(state,data){
            state.userInfo.score = data
        },
        SET_USERINFO(state,data){
            state.userInfo = data
            localStorage.setItem('userInfo',JSON.stringify(data))
        },
        SET_TOKEN(state,data){
            state.token = data;
            localStorage.setItem('token',data)
        },
        REMOVE_LOGOUT(state){
            state.token = '',
            state.userInfo = {}
            localStorage.removeItem('userInfo')
            localStorage.removeItem('token')
            $router.push('/login')
        },
        SET_HEADERIMG(state,data){
            state.headerImg = data
        }
    },
    getters:{
        urank(state){
            return state.userInfo.rank * 2
        }
    },
    actions:{
        // putUser(data){
        //     PutUserInfo(data)
        //     .then(res=>{
        //         context.commit('SET_USERINFO',res.data.user)
        //     })

        // },
        login(context,data){
            GetUserInfo(data)
            .then(res=>{
                context.commit('SET_USERINFO',res.data.user)
                context.commit('SET_TOKEN',res.data.token)
                var $route = $router.history.current
                var redirect = $route.query.redirect || '/'
                $router.replace(redirect)
            })
        },
        // 上传图片
        uploadImg(context,formData){
            UploadImg(formData)
            .then(()=>{
                context.dispatch("getImg")
            })
        },
        // 获得图片
        getImg(context){
            GetImg()
            .then(res=>{
                context.commit('SET_HEADERIMG',res.data.data[0].url) 
            })
        }
    }
}